package code.rocky.chapter02.item2;

public class BuilderNutritionFacts {

    private final int servingSize  ;
    private final int servings     ;
    private final int calories     ;
    private final int fat          ;
    private final int sodium       ;
    private final int carbohydrate ;

    public static class Builder {
        private final int servingSize;
        private final int servings;

        // Optional parameters - initialized to default values
        private int calories      = 0;
        private int fat           = 0; // default values
        private int sodium        = 0;
        private int carbohydrate  = 0;

        public Builder(int servingSize,int servings) {
            this.servingSize = servingSize;
            this.servings = servings;
        }

        public Builder calories(int val) {
            calories  = val;
            return this;
        }

        public Builder fat(int val) {
            fat = val;
            return this;
        }
        public Builder sodium(int val) {
            sodium = val;
            return this;
        }
        public Builder carbohydrate(int val) {
            carbohydrate = val;
            return this;
        }
        public BuilderNutritionFacts build() {
            return new BuilderNutritionFacts(this);
        }
    }

    private BuilderNutritionFacts(Builder builder) {
        servingSize = builder.servingSize;
        servings    = builder.servings;
        calories    = builder.calories;
        fat         = builder.fat;
        sodium      = builder.sodium;
        carbohydrate= builder.carbohydrate;
    }

    @Override
    public String toString() {
        return "BuilderNutritionFacts = {" +
                "servingSize=" + servingSize +
                ", servings=" + servings +
                ", calories=" + calories +
                ", fat=" + fat +
                ", sodium=" + sodium +
                ", carbohydrate=" + carbohydrate +
                '}';
    }

    public static void main(String[] args) {
        BuilderNutritionFacts facts = new Builder(240,200).calories(100).sodium(35).carbohydrate(27).build();
        System.out.println(facts.toString());
    }
}
